package mydb

import (
	"fmt"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
)

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

func MysqlOperation()  {
	// 使用指定驱动和数据库连接信息 打开连接
	// 数据库连接的格式：
	// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
	db, err := sql.Open("mysql", "root:123Abc!#@(192.168.0.102:3306)/test?charset=utf8")
	checkErr(err)

	rows, err := db.Query("SELECT * FROM userinfo where uid=?", 2)
	checkErr(err)

	for rows.Next()  {
		var uid int
        var username string
        var department string
        var created string
        err = rows.Scan(&uid, &username, &department, &created)
        checkErr(err)
        fmt.Print(string(uid))
        fmt.Print(username+" ")
        fmt.Print(department+" ")
		fmt.Print(created+" ")
		fmt.Println()
	}

	// 关闭DB
	defer db.Close()
}